<?php

if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class MY_Form_validation extends CI_Form_validation {

  var $ci;

  public function __construct($rules = array()) {
    parent::__construct($rules);
    $this->ci = & get_instance();
  }

  /**
   * Valid registered (signed up) email
   * 
   */
  function valid_registerd_email($email = null) {
    $get_email = $this->ci->user_model->get_email($email);
    if (!empty($email) && !empty($get_email)) {
      return true;
    } else {
      $this->set_message('valid_registerd_email', 'This email is not signed up');
      return false;
    }
  }

  /**
   * Valid unregistered (not signed up) email
   * 
   */
  function valid_unregisterd_email($email = null) {
    $get_email = $this->ci->user_model->get_email($email);
    if (!empty($email) && empty($get_email)) {
      return true;
    } else {
      $this->set_message('valid_unregisterd_email', lang('email_already_exists'));
      return false;
    }
  }

  /**
   * Validate security answer from the user security captcha
   * 
   */
  function valid_security_answer() {
    if ($this->ci->user_security->validation()) {
      return true;
    } else {
      $this->set_message('valid_security_answer', 'The %s field must contain a valid security answer.');
      return false;
    }
  }

  /**
   * Validate zip/pin code using google api
   * 
   */
  function valid_zip_pin_code($zip_code = null) {
    $user_location = $this->ci->user_location->get_location($zip_code);
    if (!empty($user_location)) {
      return $user_location['loc_key'];
    } else {
      $this->set_message('valid_zip_pin_code', 'The %s field must contain a valid zip/pin code.');
      return false;
    }
  }

}
